/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.fineract.portfolio.loanaccount.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanAccountService;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRelatedDetail;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class LoanAccrualActivityProcessingServiceImplTest {

    @InjectMocks
    private LoanAccrualActivityProcessingServiceImpl accrualActivityProcessingService;

    @Mock
    private Loan loan;

    @Mock
    private LoanProductRelatedDetail loanProductRelatedDetail;

    @Mock
    private BusinessEventNotifierService businessEventNotifierService;

    @Mock
    private LoanAccountService loanAccountService;

    @Mock
    private LoanTransactionAssembler loanTransactionAssembler;

    @Test
    void addPeriodicAccruals_ShouldNotProceed_WhenLoanIsNotOpen() {
        // Given
        final LocalDate currentDate = LocalDate.now(ZoneId.systemDefault());

        when(loan.isOpen()).thenReturn(false);

        when(loan.getLoanProductRelatedDetail()).thenReturn(loanProductRelatedDetail);
        when(loanProductRelatedDetail.isEnableAccrualActivityPosting()).thenReturn(true);

        // When
        accrualActivityProcessingService.makeAccrualActivityTransaction(loan, currentDate);

        // Then
        verify(loan, times(1)).isOpen();

        verify(loan, never()).getRepaymentScheduleInstallments(any());
        verify(loan, never()).addLoanTransaction(any());
        verify(loanAccountService, never()).saveLoanTransactionWithDataIntegrityViolationChecks(any());
        verify(loanTransactionAssembler, never()).assembleAccrualActivityTransaction(any(), any(), any());
        verify(businessEventNotifierService, never()).notifyPreBusinessEvent(any());
        verify(businessEventNotifierService, never()).notifyPostBusinessEvent(any());
    }

    @Test
    void addPeriodicAccruals_ShouldProceed_WhenLoanIsOpen() {
        // Given
        final LocalDate currentDate = LocalDate.now(ZoneId.systemDefault());

        when(loan.isOpen()).thenReturn(true);

        when(loan.getLoanProductRelatedDetail()).thenReturn(loanProductRelatedDetail);
        when(loanProductRelatedDetail.isEnableAccrualActivityPosting()).thenReturn(true);
        when(loan.getRepaymentScheduleInstallments()).thenReturn(new ArrayList<>());

        // When
        accrualActivityProcessingService.makeAccrualActivityTransaction(loan, currentDate);

        // Then
        verify(loan, times(1)).isOpen();

        verify(loan).getRepaymentScheduleInstallments(any());
        verify(loan, never()).addLoanTransaction(any());
        verify(loanAccountService, never()).saveLoanTransactionWithDataIntegrityViolationChecks(any());
        verify(loanTransactionAssembler, never()).assembleAccrualActivityTransaction(any(), any(), any());
        verify(businessEventNotifierService, never()).notifyPreBusinessEvent(any());
        verify(businessEventNotifierService, never()).notifyPostBusinessEvent(any());
    }

}
